/*
    This file is part of Posixcpp library.

    Posixcpp library is free software: you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public License
    as published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    Posixcpp library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU LesserGeneral Public
    License along with Posixcpp library. If not, see
    <http://www.gnu.org/licenses/>.
 */

#ifndef POSIX_COMONIONODE_HPP_INCLUDED
#define POSIX_COMONIONODE_HPP_INCLUDED

#include "posix_io.hpp"

namespace posix
{
namespace io
{
/**
 * Default numer of receive loop iterations to receive requested data size
 */
extern const unsigned DEFAULT_RECEIVETRIES;
/**
 * Default numer of send loop iterations to send requested data size
 */
extern const unsigned DEFAULT_SENDTRIES;

/**
 * @class File
 * @brief Abstract class, implements common operations for ordinar files and sockets
 */
class Handle : public IHandle
{
public:
    Handle( BlockingMode_t bMode = BlockingMode_t::NON_BLOCK );
    Handle(int descriptor);
    virtual ~Handle();

    void close();

    void read(
      posixcpp::ByteArray & data,
      IOMode_t mode = IOMode_t::SINGLE,
      time::Timespec timeout = -1000000 ) override;
    ssize_t write(
      const posixcpp::ByteArray & data,
      IOMode_t mode = IOMode_t::SINGLE,
      time::Timespec timeout = -1000000 ) override;
    Events poll(
      Events events,
      time::Timespec timeout = -1000000 ) override;
    /**
     * @property Descriptor
     * @return POSIX file/socket descriptor
     */

    int descriptor() const {
        return m_descriptor;
    }
    void setDescriptor(int newVal) {
        m_descriptor = newVal;
    }
    /**
     * @property ReadTries
     * @brief Maximal number of receive iterations for looped mode
     */
    
    unsigned readTries() const {
        return m_readTries;
    }
    void setReadTries(unsigned newVal) {
        m_readTries = newVal;
    }
    
    /**
     * @property WriteTries
     * @brief Maximal number of write iterations for looped mode
     */
    
    unsigned writeTries() const {
        return m_sendTries;
    }
    void setWriteTries(unsigned newVal) {
        m_sendTries = newVal;
    }
    
    /**
     * @property blockingMode
     */
    
    BlockingMode_t blockingMode() const {
        return m_blockingMode;
    }
    void setBlockingMode( BlockingMode_t newVal ) {
        m_blockingMode = newVal;
    }
    
protected:
    /**
     * File/Socket descriptor
     */
    int m_descriptor;
    /**
     * Number of the looped mode receive tries
     */
    unsigned m_readTries;
    /**
     * Number of the looped mode send tries
     */
    unsigned m_sendTries;
    /**
     * Blocking mode
     */
    BlockingMode_t m_blockingMode;
};

}
}

#endif // POSIX_COMONIONODE_HPP_INCLUDED
